import torch

from torch.utils.data import Dataset,DataLoader

data_path = "./data/SMSSpamCollection"

# 完成数据集类

class MyDataset(Dataset):
    def __init__(self):
        self.lines = open(data_path,encoding="utf-8").readlines()



    def __getitem__(self, index): # 对象[索引] 对应调用

        # return self.lines[index]
        cur_line = self.lines[index].strip()

        label = cur_line[:4].strip()
        content = cur_line[4:].strip()
        return label,content
    def __len__(self):  # len(对线) 对应调用
        return len(self.lines)




my_dataset = MyDataset()
data_loader = DataLoader(dataset=my_dataset,batch_size=2,shuffle=True,num_workers=2)



if __name__ == "__main__":


    # my_dataset = MyDataset()
    #
    # print(my_dataset[1000])
    # print(len(my_dataset))

    # for a,b in data_loader:  # 不使用enumerate 后无法使用index
    #     print(a,b)


    # for index, (label, context) in enumerate(data_loader):
    #     print(index, label, context)
    #     print("*" * 100)

    for i in data_loader:
        print(i)
        break

    print(len(my_dataset))
    print(len(data_loader))